package com.qiaoan.sys.security;

import com.qiaoan.back.model.sys.UserDetail;
import com.qiaoan.back.service.sys.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
/**  
 * 该类是用户信息的定义和验证  
 * 这个类主要是处理用户登录信息，在用户输入用户名和密码后，  
 * spring security会带着用户名调用类里面的loadUserByUsername(usrename)方法，  
 * 通过用户名查出用户信息，然后把数据库中查出的用户密码和刚刚用户输入的存储在session中的密码做比较，然后判断该用户是否合法！   
 *  
 */
@Service("customUserDetailsService")
public class CustomUserDetailsService implements UserDetailsService{

	private static final Logger logger = LogManager.getLogger();
	@Autowired
	private UserService userService ;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		logger.info("######################loadUserByUsername%%%%%%%%%%%" +username);
		ArrayList<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
		if(username.compareTo("user")==0){
			logger.info("&&&&&&&&&&&&&&&&&&&&&7777user");
			authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
			return new UserDetail(username, "123456",authorities);
		}else if(username.compareTo("admin")==0){
			logger.info("&&&&&&&&&&&&&&&&&&&&&admin");
			authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
			return new UserDetail(username, "123456",authorities);
		}
		logger.info("&&&&&&&&&&&&&&&&&&&&&null");
		return null;
	}
//	@Override
//	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//		System.out.println("######################loadUserByUsername%%%%%%%%%%%");
//		//根据登录名获取登陆用户
//		UserModel user = new UserModel();
//		user.setUsername("zhangchong");
//		user.setPassword("zhangchong");
//				//userDao.getUserByusername(username);
//		if(user == null){
//			throw new UsernameNotFoundException("用户" + username + "不存在");
//		}
//
//		//根据用户的 类型获取用户权限
//
//		//用户类型为0, 表示是系统平台管理员用户
//
//		//团队管理员
//
//		//团队用户
//
//		/**/
//		//根据用户的 类型获取用户权限
//        if(user.getUserType() == UserType.SysAdmin){ //用户类型为0, 表示是系统平台管理员用户
//            Collection<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
//            List<Authority> authorities = authorityDao.findAll();  //获取系统中所有权限
//            for(Iterator<Authority> iter =authorities.iterator();iter.hasNext();){
//                auths.add(new SimpleGrantedAuthority(iter.next().getCode()));
//            }
//            //UserDetail userDetail =  new UserDetail(userName, user.getPassword(), true, true, true, true, auths);
//            //修改为前台系统，不能让系统管理员登陆
//            UserDetail userDetail =  new UserDetail(userName, user.getPassword(), false, true, true, true, auths);
//            userDetail.setUserGroupId(user.getUserGroup().getId());
//            userDetail.setRealName(user.getRealName());
//            userDetail.setUserType(user.getUserType());
//            return userDetail;
//        }else if(user.getUserType() == UserType.TeamAdmin){ //团队管理员
//            //用户是否可用
//            boolean enabled = user.getEnabled();
//            //获取团队管理员的所有权限
//            Set<Authority> authorities = user.getAuthoritySet();
//            Collection<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
//            for(Iterator<Authority> iter=authorities.iterator();iter.hasNext();){
//                auths.add(new SimpleGrantedAuthority(iter.next().getCode()));
//            }
//            //因为spring security包中的User也实现了UserDetails，所以也可以直接返回User
//            UserDetail userDetail =null;
//            if(UserState.Enable == user.getUserState()){
//                userDetail = new UserDetail(userName, user.getPassword(), enabled, true, true, true, auths);
//            }else{
//                userDetail = new UserDetail(userName, user.getPassword(), enabled, true, true, false, auths);
//                //throw new LockedException("用户" + userName + "被锁定");
//            }
//            userDetail.setUserGroupId(user.getUserGroup().getId());
//            userDetail.setTeamGroupId(user.getUserGroup().getId());
//            userDetail.setRealName(user.getRealName());
//            userDetail.setUserType(user.getUserType());
//            return userDetail;
//
//        }else{//团队用户
//            //用户是否可用
//            boolean enabled = user.getEnabled();
//            Set<Authority> authorities = user.getAuthoritySet();
//            Collection<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
//            for(Iterator<Authority> iter=authorities.iterator();iter.hasNext();){
//                auths.add(new SimpleGrantedAuthority(iter.next().getCode()));
//            }
//            //因为spring security包中的User也实现了UserDetails，所以也可以直接返回User
//            UserDetail userDetail =null;
//
//            if(user.getUserGroup().getTeamAdmin().getUserState()==UserState.Disable){//如果团队管理员被禁用，其下属团队用也不能登录
//                userDetail = new UserDetail(userName, user.getPassword(), enabled, true, true, false, auths);
//                return userDetail;
//            }
//
//            if(UserState.Enable == user.getUserState()){
//                userDetail = new UserDetail(userName, user.getPassword(), enabled, true, true, true, auths);
//            }else{
//                userDetail = new UserDetail(userName, user.getPassword(), enabled, true, true, false, auths);
//                //throw new LockedException("用户" + userName + "被锁定");
//            }
//            userDetail.setUserGroupId(user.getUserGroup().getId());
//            userDetail.setTeamGroupId(user.getUserGroup().getTeamAdmin().getUserGroup().getId());
//            userDetail.setRealName(user.getRealName());
//            userDetail.setUserType(user.getUserType());
//            return userDetail;
//
//        }
//		Collection<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
//		return new UserDetail(user.getUsername(),user.getPassword(),auths);
//
//    }

}
